/**
 * $Id: OpenEmployeeEditor.java 7 2011-03-09 07:13:54Z r.middel@gmail.com $
 *
 * Copyright (C) 2010 mrBussy.eu
 * 
 * WickedHRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WickedHRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.applications.wickedhrm.gui.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import eu.mrbussy.applications.wickedhrm.data.Employee;
import eu.mrbussy.applications.wickedhrm.gui.editors.EmployeeEditor;
import eu.mrbussy.applications.wickedhrm.gui.editors.EmployeeEditorInput;
import eu.mrbussy.applications.wickedhrm.gui.view.OrganisationTreeView;

public class OpenEmployeeEditor extends AbstractHandler  {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();	
		// Get the view
		OrganisationTreeView view = (OrganisationTreeView) page.findView(OrganisationTreeView.ID);
		// Get the selection
		if((view.getSite()!=null)&&(view.getSite().getSelectionProvider()!=null))
		{
			ISelection selection = view.getSite().getSelectionProvider().getSelection();
			if (selection != null && selection instanceof IStructuredSelection) {
				Object obj = ((IStructuredSelection) selection).getFirstElement();
				// If we had a selection lets open the editor
				if (obj != null) {
					Employee emp = (Employee) obj;
					EmployeeEditorInput input = new EmployeeEditorInput(
							// TODO: change to string handler
							emp);
					try {
						page.openEditor(input, EmployeeEditor.ID);
	
					} catch (PartInitException e) {
						throw new RuntimeException(e);
					}
				}
				else
				{
					try {
						System.out.println("Create employee");
						page.openEditor(null, EmployeeEditor.ID);
					} catch (PartInitException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			else
			{
				try {
					System.out.println("Create employee");
					page.openEditor(null, EmployeeEditor.ID);
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return null;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}
}
